﻿using System;
using System.Collections.Generic;
using System.Data;
using BLL;
using Utils;
namespace UI.ModEmployee
{
    public partial class EmpPassedAdd : System.Web.UI.Page
    {
        EmpPassedBiz empPassedBiz = new EmpPassedBiz();
        EmployeeBiz employeeBiz = new EmployeeBiz();
        DefDicBiz defDicBiz = new DefDicBiz();
        string passedID = "0";
        string account = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["user"] != null)
            {
                User user = Session["user"] as User;
                account = user.Account;
            }
            else
            {
                JSUtility.Alert(this, "您没有登录!请先登录。");
                return;
            }

            if (Request.Params["PassedID"] != null)
            {
                passedID = Request.Params["PassedID"].ToString();
                btnAdd.Enabled = false;
            }
            else
            {
                btnUpdate.Enabled = false;
                btnDelete.Enabled = false;
            }
            if (!IsPostBack)
            {
                defDicBiz.InitDropDownList("Admin_Passed_type", ddlPassedTypeName, "");
                defDicBiz.InitXMLDropDownList(ddlInfoType, "PassedInfoType.xml", "");
                JSUtility.AddConfirm(btnDelete, "您真的要删除该信息吗？");
                txtPassedTime.Text = System.DateTime.Now.ToString("yyyy-MM-dd");
                if (passedID != "0") BindData(passedID);

            }
        }
        private void BindData(string passedID)
        {
            DataTable dt = empPassedBiz.GetOneInfo(passedID);
            if (dt.Rows.Count > 0)
            {
                txtEmpCode.Text = dt.Rows[0]["EmpCode"].ToString();
                txtOldDeptName.Text = dt.Rows[0]["OldDeptName"].ToString();
                txtOldDutyName.Text = dt.Rows[0]["OldDutyName"].ToString();
                txtOldAreaName.Text = dt.Rows[0]["OldAreaName"].ToString();
                txtEmpName.Text = dt.Rows[0]["EmpName"].ToString();

                defDicBiz.InitDropDownList("Admin_Passed_type", ddlPassedTypeName, dt.Rows[0]["PassedTypeName"].ToString());
                defDicBiz.InitDropDownList("Admin_Emp_Duty", ddlNewDutyName, dt.Rows[0]["NewDutyName"].ToString());
                defDicBiz.InitDropDownList(ddlInfoType, dt.Rows[0]["InfoTypeName"].ToString());
                string JoinTime = dt.Rows[0]["JoinTime"].ToString();
                if (Utils.RegexRule.IsDate(JoinTime))
                    txtJoinTime.Text = Convert.ToDateTime(JoinTime).ToString("yyyy-MM-dd");
                string passedTime = dt.Rows[0]["PassedTime"].ToString();
                if (Utils.RegexRule.IsDate(passedTime))
                    txtPassedTime.Text = Convert.ToDateTime(passedTime).ToString("yyyy-MM-dd");

                txtMemo.Text = dt.Rows[0]["Memo"].ToString();
                dt.Dispose();
            }
            else
            {
                JSUtility.Alert(this, "无数据");
            }

        }


        protected void txtEmpCode_TextChanged(object sender, EventArgs e)
        {
            string empCode = txtEmpCode.Text.Trim();
            if (!string.IsNullOrWhiteSpace(empCode))
            {
                if (Utils.StringUtility.IncludeHZ(empCode))//如果包含汉字
                {
                    DataTable dt = employeeBiz.GetOneInfoByName(empCode);
                    if (dt.Rows.Count == 0)
                    {
                        JSUtility.Alert(this, "不存在该员工姓名: " + empCode);
                    }
                    else if (dt.Rows.Count == 1)
                    {
                        if (dt.Rows[0]["StateName"].ToString() != "离职")
                        {
                            txtEmpCode.Text = dt.Rows[0]["EmpCode"].ToString();
                            txtOldDeptName.Text = dt.Rows[0]["DeptName"].ToString();
                            txtOldDutyName.Text = dt.Rows[0]["DutyName"].ToString();
                            txtEmpName.Text = dt.Rows[0]["EmpName"].ToString();
                            txtOldAreaName.Text = dt.Rows[0]["AreaName"].ToString();
                            string JoinTime = dt.Rows[0]["JoinTime"].ToString();
                            if (Utils.RegexRule.IsDate(JoinTime))
                                txtJoinTime.Text = Convert.ToDateTime(JoinTime).ToString("yyyy-MM-dd");
                            defDicBiz.InitDropDownList("Admin_Emp_Duty", ddlNewDutyName, dt.Rows[0]["DutyName"].ToString());

                        }
                        else
                            JSUtility.Alert(this, "该员工已经离职: " + empCode);
                    }
                    else
                    {
                        string info = "存在同名，请输入工号：\n\n";
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            info += dt.Rows[i]["EmpCode"].ToString() + "  " + dt.Rows[i]["EmpName"].ToString() + "\n";
                        }
                        JSUtility.Alert(this, info);
                    }
                }
                else
                {
                    DataTable dt = employeeBiz.GetOneInfo(empCode);
                    if (dt.Rows.Count > 0)
                    {
                        txtOldDeptName.Text = dt.Rows[0]["DeptName"].ToString();
                        txtOldDutyName.Text = dt.Rows[0]["DutyName"].ToString();
                        txtEmpName.Text = dt.Rows[0]["EmpName"].ToString();
                        txtOldAreaName.Text = dt.Rows[0]["AreaName"].ToString();
                        string JoinTime = dt.Rows[0]["JoinTime"].ToString();
                        if (Utils.RegexRule.IsDate(JoinTime))
                            txtJoinTime.Text = Convert.ToDateTime(JoinTime).ToString("yyyy-MM-dd");
                        defDicBiz.InitDropDownList("Admin_Emp_Duty", ddlNewDutyName, dt.Rows[0]["DutyName"].ToString());
                    }
                    else
                        JSUtility.Alert(this, "不存在该员工编号: " + empCode);
                }
            }
            btnUpdate.Enabled = false;
            btnDelete.Enabled = false;
        }
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (!AccountBiz.CheckRight(account, "EmpPassedAdd.aspx", RightChar.Add))
            {
                if (!AccountBiz.CheckRight(account, "EmpPassedList.aspx", RightChar.Add))
                {
                    JSUtility.Alert(this, "无权限！");
                    return;
                }
            }
            if (ddlPassedTypeName.SelectedValue == "0")
            {
                JSUtility.Alert(this, "请选择转正类别");
                return;
            }
            string empCode = txtEmpCode.Text.Trim();
            if (string.IsNullOrWhiteSpace(empCode))
            {
                JSUtility.Alert(this, "员工编号必须填写");
                return;
            }
            if (!employeeBiz.IsExistEmpCode(empCode))
            {
                JSUtility.Alert(this, "员工编号不存在");
                return;
            }

            string joinTime = txtJoinTime.Text.Trim();
            string passedTime = txtPassedTime.Text.Trim();
            if (!RegexRule.IsDate(joinTime))
            {
                JSUtility.Alert(this, "入职日期格式错误");
                return;
            }
            if (!RegexRule.IsDate(passedTime))
            {
                JSUtility.Alert(this, "通过日期格式错误");
                return;
            }
          string passedTypeName=  ddlPassedTypeName.SelectedItem.Text.Trim();
            string infoTypeName=ddlInfoType.SelectedValue;
            SortedDictionary<string, string> sdList = new SortedDictionary<string, string>();

            sdList.Add("EmpCode", empCode);
            sdList.Add("EmpName", txtEmpName.Text.Trim());
            sdList.Add("OldDutyName", txtOldDutyName.Text.Trim());
            sdList.Add("OldDeptName", txtOldDeptName.Text.Trim());
            sdList.Add("OldAreaName", txtOldAreaName.Text.Trim());
            sdList.Add("NewDutyName", ddlNewDutyName.SelectedItem.Text.Trim());
            sdList.Add("PassedTypeName", passedTypeName);
            sdList.Add("JoinTime", joinTime);
            sdList.Add("PassedTime", passedTime);
            sdList.Add("InfoTypeName", infoTypeName);
            sdList.Add("Memo", txtMemo.Text.Trim());
            sdList.Add("Account", account);      
                
                    //更改员工状态
                    SortedDictionary<string, string> hList = new SortedDictionary<string, string>();
                    hList.Add("EmpCode", txtEmpCode.Text.Trim());
                    hList.Add("DutyID", ddlNewDutyName.SelectedValue);
                    hList.Add("StateID", "1");//字典中的ID
                    if (infoTypeName == "入职")
                    hList.Add("TestEndTime", passedTime);//转正时间
                    else if (infoTypeName == "晋升")
                        hList.Add("TestEndTime2", passedTime);//转正时间                  

                    string info = "增加转正信息成功";
                    if (empPassedBiz.Insert(sdList, hList))
                    {
                        JSUtility.AlertAndRedirect(info, "EmpPassedAdd.aspx");
                    }
                    else
                        JSUtility.Alert(this, "增加失败");
        }
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            if (!AccountBiz.CheckRight(account, "EmpPassedAdd.aspx", RightChar.Mod))
            {
                if (!AccountBiz.CheckRight(account, "EmpPassedList.aspx", RightChar.Mod))
                {
                    JSUtility.Alert(this, "无权限！");
                    return;
                }
            }
            if (ddlPassedTypeName.SelectedValue == "0")
            {
                JSUtility.Alert(this, "请选择转正类别");
                return;
            }
            string empCode = txtEmpCode.Text.Trim();
            if (string.IsNullOrWhiteSpace(empCode))
            {
                JSUtility.Alert(this, "员工编号必须填写");
                return;
            }
            if (!employeeBiz.IsExistEmpCode(empCode))
            {
                JSUtility.Alert(this, "员工编号不存在");
                return;
            }
            string joinTime = txtJoinTime.Text.Trim();
            string passedTime = txtPassedTime.Text.Trim();
            if (!RegexRule.IsDate(joinTime))
            {
                JSUtility.Alert(this, "入职日期格式错误");
                return;
            }
            if (!RegexRule.IsDate(passedTime))
            {
                JSUtility.Alert(this, "通过日期格式错误");
                return;
            }
            string infoTypeName = ddlInfoType.SelectedValue;
            SortedDictionary<string, string> sdList = new SortedDictionary<string, string>();
            sdList.Add("PassedID", passedID);
            sdList.Add("EmpCode", empCode);
            sdList.Add("EmpName", txtEmpName.Text.Trim());
            sdList.Add("OldDutyName", txtOldDutyName.Text.Trim());
            sdList.Add("OldDeptName", txtOldDeptName.Text.Trim());
            sdList.Add("OldAreaName", txtOldAreaName.Text.Trim());
            sdList.Add("NewDutyName", ddlNewDutyName.SelectedItem.Text.Trim());
            sdList.Add("PassedTypeName", ddlPassedTypeName.SelectedItem.Text.Trim());
            sdList.Add("JoinTime", joinTime);
            sdList.Add("PassedTime", passedTime);
            sdList.Add("InfoTypeName", infoTypeName);
            sdList.Add("Memo", txtMemo.Text.Trim());
            sdList.Add("Account", account);
            string info = "";
            if (empPassedBiz.Update("Admin_Emp_Passed", sdList))
            {
                info = "更新转正信息成功";
               
               
                    //更改员工状态
                    SortedDictionary<string, string> hList = new SortedDictionary<string, string>();
                    hList.Add("EmpCode", txtEmpCode.Text.Trim());
                    hList.Add("DutyID", ddlNewDutyName.SelectedValue);
                    hList.Add("StateID", "1");//字典中的ID
                    if (infoTypeName == "入职")
                    hList.Add("TestEndTime", passedTime);//转正时间
                    else if (infoTypeName == "晋升")
                        hList.Add("TestEndTime2", passedTime);//转正时间
                    if (employeeBiz.Update("Admin_Employee", hList)) info += "\n更员工新信息成功";
                    else info += "\n更新员工信息失败";
              
            }
            else
                info = "更新转正信息失败";
            JSUtility.Alert(this, info);
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (!AccountBiz.CheckRight(account, "EmpPassedAdd.aspx", RightChar.Del))
            {
                if (!AccountBiz.CheckRight(account, "EmpPassedList.aspx", RightChar.Del))
                {
                    JSUtility.Alert(this, "无权限！");
                    return;
                }
            }
            if (passedID != "0")
            {
                if (empPassedBiz.DeleteOneInfo(passedID))
                    JSUtility.Alert(this, "删除成功");
                else
                    JSUtility.Alert(this, "删除失败");
            }
            else
                JSUtility.Alert(this, "请先选择数据");
        }

        protected void btnClose_Click(object sender, EventArgs e)
        {
            JSUtility.CloseWindow();
        }

    }
}